package testtiles;

import java.awt.Color;
import java.awt.Graphics2D;

import butines.core.game.BasicGame;
import butines.core.game.Drawing;
import butines.core.game.ImageSheet;
import butines.core.game.Input;

public class TestUnitGame extends BasicGame {

	private Resources resources;
	private boolean pause = true;
	
	private Unit unit0;
	private Unit unit1;
	private Unit unit2;
	private Unit unit3;
	private Unit unit4;
	
	private World world;
	
	public static void main(String[] args) {
		new TestUnitGame().start();
	}

	@Override
	public void update(float tpf) {
		if (pause) {
			return;
		}
		world.update(tpf);
	}

	@Override
	public void render(Graphics2D g2d) {
		Drawing.clear(Color.BLACK);
		world.draw(g2d);
	}

	@Override
	protected void initialise() {
		
		resources = new Resources("datas/game.properties");
		
		
		ImageSheet tileset = resources.getTileset("tileset.summer");
		
		Map map = new Map(tileset, 32, 32);
		map.setVisibleTiles(0, 0, 320, 320);
		
		world = new World(map);
		world.setTargetFps(gameContext.getTargetFps());
		
		UnitClass unitClass = resources.getUnitClass("unitClass.footman");
		
		unit0 = world.createUnit(unitClass);
		unit0.setPosition(160, 160);
		unit0.changeState(UnitHoldState.getInstance());
		
		unit1 = world.createUnit(unitClass);
		unit1.setPosition(0, 0);
		unit1.setTargetPoint(170, 170);
		unit1.changeState(UnitMoveState.getInstance());

		unit2 = world.createUnit(unitClass);
		unit2.setPosition(30, 40);
		unit2.setTargetPoint(30, 240);
		unit2.changeState(UnitMoveState.getInstance());
		
		unit3 = world.createUnit(unitClass);
		unit3.setPosition(60, 40);
		unit3.setTargetPoint(30, 240);
		unit3.changeState(UnitMoveState.getInstance());
		
		unit4 = world.createUnit(unitClass);
		unit4.setPosition(260, 40);
		unit4.setTargetPoint(260, 260);
		unit4.changeState(UnitMoveState.getInstance());

		Unit unit  = null;
		unit = world.createUnit(unitClass);
		unit.setPosition(192, 192);
		unit.changeState(UnitHoldState.getInstance());
		
		unit = world.createUnit(unitClass);
		unit.setPosition(128, 128);
		unit.changeState(UnitHoldState.getInstance());
		

//		unit = world.createUnit(unitClass);
//		unit.setPosition(160, 128);
//		unit.changeState(UnitHoldState.getInstance());
//		
//
//		unit = world.createUnit(unitClass);
//		unit.setPosition(128, 160);
//		unit.changeState(UnitHoldState.getInstance());
		
		world.update(0);
	}

	@Override
	protected void release() {
		resources.release();
	}

	@Override
	public void onKey(int keyCode, boolean pressed, float tpf) {
		if (keyCode == Input.KEY_ESC) {
			gameContext.shutdown();
		}
		if (keyCode == Input.KEY_SPACE && pressed) {
			pause = !pause;
		}
	}

	@Override
	public void onButton(int x, int y, int button, boolean pressed, float tpf) {
	}

	@Override
	public void onMove(int x, int y, int dx, int dy, float tpf) {
	}

	@Override
	public void onWheel(int x, int y, int wheel, float tpf) {
	}

}
